package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceRecordAddDTO;
import org.jsola.hr.dto.AttendanceRecordUpdateDTO;
import org.jsola.hr.query.AttendanceRecordQuery;
import org.jsola.hr.service.IAttendanceRecordService;
import org.jsola.hr.vo.AttendanceRecordListVO;
import org.jsola.hr.vo.AttendanceRecordVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrAttendanceRecordController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendanceRecords")
public class AttendanceRecordController implements IAttendanceRecordControllerAPI {

    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    @Override
    @PostMapping
    public Result<AttendanceRecordVO> save(@Valid @RequestBody AttendanceRecordAddDTO attendanceRecordAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工考勤记录权限校验

        return Result.success(
                attendanceRecordService.save(attendanceRecordAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendanceRecordUpdateDTO attendanceRecordUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工考勤记录权限校验

        int count = attendanceRecordService.update(attendanceRecordUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendanceRecordId}")
    public Result<Integer> delete(@PathVariable String attendanceRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工考勤记录权限校验

        int count = attendanceRecordService.deleteByIds(tokenUser, attendanceRecordId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendanceRecordIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工考勤记录权限校验

        int count = attendanceRecordService.deleteByIds(tokenUser, attendanceRecordIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendanceRecordId}")
    public Result<AttendanceRecordVO> findById(@PathVariable String attendanceRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤记录权限校验

        return Result.success(
            attendanceRecordService.selectById(attendanceRecordId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendanceRecordListVO>> listAll(@Valid AttendanceRecordQuery attendanceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤记录权限校验

        return Result.success(
            attendanceRecordService.select(attendanceRecordQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendanceRecordListVO>> page(@Valid AttendanceRecordQuery attendanceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendanceRecordService.selectPage(attendanceRecordQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/attendanceRecordImportExcel", headers = "content-type=multipart/form-data")
    public Result<Boolean> attendanceRecordImportExcel(@RequestParam(value = "file",required = false) MultipartFile file) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工考勤记录权限校验

        return Result.success(
                attendanceRecordService.attendanceRecordImportExcel(tokenUser, file)
        );
    }


}

